package com.szyjjk.controller;

import com.alibaba.fastjson.JSONObject;
import com.szyjjk.constants.SzyjjkConstant;
import com.szyjjk.entity.ResponseBo;
import com.szyjjk.entity.UserBo;
import com.szyjjk.service.UserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.Date;
import java.util.List;
import java.util.Set;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;


    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 跳转到登陆界面
     * @Date 15:14 2020/2/21
     * @Param [response]
     **/
    @RequestMapping(value = "/login.do")
    public String login(HttpServletResponse response) {

        return "login";
    }

    @RequestMapping(value = "/loginVerify.do")
    @ResponseBody
    public ResponseBo loginVerify(HttpServletRequest request, String userName,String passWord) {
        ResponseBo rb = new ResponseBo();
        List<UserBo> userBos=userService.loginQuery(userName,passWord);
        if(userBos.size()>0){
            rb.setCode(1);
        }else{
            rb.setCode(0);
        }
        HttpSession session = request.getSession();
		session.setAttribute(SzyjjkConstant.TOKEN,userName); //往session里面存储用户信息
        if(userBos.size() == 1){
            session.setAttribute("userinfo",userBos.get(0));
        }else{
            new Exception("查询出多个用户信息，信息错误");
        }
		session.setAttribute("cssid","runwork"); //往session里面存储用户信息
		//默认7天 604800  1天 86400
		session.setMaxInactiveInterval(86400);
        return rb;
    }

    @RequestMapping(value = "/loginOut.do")
    public String logoutPage(HttpServletRequest request,HttpServletResponse response) {

        request.getSession().removeAttribute(SzyjjkConstant.TOKEN);
        return "login";
    }

//    @RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
//    @ResponseBody
//    public JSONObject test(HttpServletRequest request) {
//        JSONObject resultJsonObjet = new JSONObject();
//        resultJsonObjet.put("a", "???居然可以了？");
//        return resultJsonObjet;
//    }
}
